package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MainActivity;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.fragment.LineAppraiseFragment;
import where2go.sctu.com.zhiyou.fragment.LineIntroFragment;
import where2go.sctu.com.zhiyou.fragment.LinePlanFragment;
import where2go.sctu.com.zhiyou.utils.C;
import where2go.sctu.com.zhiyou.view.ImageBarnnerFramLayout;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.activity
 * Created by 张某人
 * CreatedTime:on 2018/5/7   23:07
 * Description: 线路详情activity
 */

public class LineDetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageBarnnerFramLayout mGroup;

    private LineIntroFragment lineIntroFragment;
    private LinePlanFragment planFragment;
    private LineAppraiseFragment lineAppraiseFragment;
    private RadioGroup radioGroup;
    private ImageView imageView;
    private LinearLayout linearLayout;

    private LinearLayout enshrineLinear;
    private LinearLayout consultLinear;
    private TextView studyTv;



    //存放轮播图片
    private int[] ids = new int[]{
            R.drawable.img7, R.drawable.img8, R.drawable.img9,R.drawable.img10
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.line_details_layout);

        initView();
        changeFragment(new LineIntroFragment().getLineIntroFragment());



    }

    private void setIndexSelectedTwo(int index) {
        switch (index) {
            case 0:
                linearLayout.setVisibility(View.VISIBLE);
                changeFragment(new LineIntroFragment().getLineIntroFragment());
                break;
            case 1:
                linearLayout.setVisibility(View.GONE);
                changeFragment(new LinePlanFragment().getLinePlanFragment());
                break;
            case 2:
                linearLayout.setVisibility(View.GONE);
                changeFragment(new LineAppraiseFragment().getLineAppraiseFragment());
                break;
            default:
                break;
        }
    }

    //方法二，默认第一fragment
    private void changeFragment(Fragment fm) {
        Intent intent=getIntent();
        String classId=  intent.getStringExtra("classId");
        Bundle bundle=new Bundle();
        bundle.putString("id",classId);
        fm.setArguments(bundle);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction.replace(R.id.detail_fragment, fm);
        transaction.commit();
    }

    private void initView() {
        consultLinear=(LinearLayout) findViewById(R.id.consult);
        enshrineLinear=(LinearLayout) findViewById(R.id.enshrine);
        studyTv=(TextView) findViewById(R.id.immediate_study);
        consultLinear.setOnClickListener(this);
        enshrineLinear.setOnClickListener(this);
        studyTv.setOnClickListener(this);

        linearLayout=(LinearLayout) findViewById(R.id.line_linear);
        mGroup = (ImageBarnnerFramLayout) findViewById(R.id.detail_image_group);
        imageView=(ImageView) findViewById(R.id.detail_back);
        imageView.setOnClickListener(this);
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int arg1) {
                //遍历RadioGroup 里面所有的子控件。
                for (int index = 0; index < group.getChildCount(); index++) {
                    //获取到指定位置的RadioButton
                    RadioButton rb = (RadioButton)group.getChildAt(index);
                    //如果被选中
                    if (rb.isChecked()) {
                        setIndexSelectedTwo(index);  //方法二
                        break;
                    }
                }
            }
        });

        //轮播图
        //我们需要求出我们当前手机的宽度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        C.WIDTH = dm.widthPixels;

        List<Bitmap> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), ids[i]);
            list.add(bitmap);
        }
        mGroup.addBitmaps(list);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.detail_back:
                Intent intent =new Intent();
                intent.setClass(LineDetailsActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.enshrine:
                getEnshrine();
                break;
            case R.id.immediate_study:
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.detail_fragment,new LinePlanFragment(),null)
                        .commit();
                break;
            case R.id.consult:
                startActivity(new Intent(LineDetailsActivity.this,
                        HelpFeedbackActivity.class));
                break;
                default:
                    break;
        }

    }

    private void getEnshrine() {

    }
}
